import { ajax } from "@/utils/ajax";

// 学生类型定义
export interface Student {
    id?: number;
    studentNumber?:string;
    name?: string;
    age?: number;
    sex?: number; // 1男，0女
    birthday?: string;
    idCard?: string;
    phone?: string;
    createTime?: string;
    grade?: string;
    department?: string;
    major?: string;
    groupId?: number;
    groupName?: string;
    blackType?: number;
    password?: string;
}

// 新增学生
export interface StudentForAdd {
    studentNumber?:string;
    name?: string;
    idCard?: string;
    phone?: string;
    grade?: string;
    department?: string;
    major?: string;
}

// 编辑学生
export interface StudentForEdit {
    id?: number;
    studentNumber?:string;
    name?: string;
    idCard?: string;
    phone?: string;
    createTime?: string;
    grade?: string;
    department?: string;
    major?: string;
    groupId?: number;
}

// 评价
export interface Evaluation {
    id?: number;
    studentId: number;
    teacherId: number;
    evaluation: string;
    createTime?: string;
    teacherName?: string;
    studentName?: string;
}

// 获取学生所在小组的指导老师
export const getStudentGroupTeachers = (groupId: number) => {
    return ajax.get("/student/getTeacher", {
        params: { groupId }
    });
};

// 获取学生的评价记录
export const getStudentEvaluations = (studentId: number) => {
    return ajax.get("/student/getEvaluation", {
        params: { studentId }
    });
};

// 添加对学生评价
export const addStudentEvaluation = (data: Evaluation) => {
    return ajax.post("/student/addEvaluation", data);
};

// 院系专业
export interface DataModel {
    id: number;
    department: string;
    major: string;
}

// 事件日志
export interface EventLog {
    id: number;
    studentId: number;
    groupId: number;
    groupName: string;
    logType: number; // 1:加入小组, 2:退出小组
    createTime: string;
    log?: string;
}

// 拉黑学生
export interface BlackStudentParams {
    studentId: number;
    reason: string;
    groupId?: number;
}

// 添加API响应接口
export interface ApiResponse<T = any> {
    code: number;
    msg: string;
    data: T;
}

// 获取学生列表
export const getStudentList = (params: {
    pageNum?: number;
    pageSize?: number;
    name?: string;
    idCard?: string;
    phone?: string;
    grade?: string;
    department?: string;
    major?: string;
    sex?: number;
    createTimeStart?: string;
    createTimeEnd?: string;
}) => {
    return ajax.get("/student/getList", {
        params
    });
};

// 根据ID获取学生
export const getStudentById = (id: number) => {
    return ajax.get("/student/get", {
        params: { id }
    });
};

// 添加学生
export const addStudent = (data: StudentForAdd) => {
    return ajax.post("/student/add", data);
};

// 编辑学生
export const editStudent = (data: StudentForEdit) => {
    return ajax.post("/student/edit", data);
};

// 删除学生
export const deleteStudent = (id: number) => {
    return ajax.delete("/student/delete", {
        params: { id }
    });
};

// 拉黑学生
export const blackStudent = (data: BlackStudentParams) => {
    return ajax.post("/black/add", {
        reason: data.reason,
        studentId: data.studentId
    },
        {
        params: {
            studentId: data.studentId,
            groupId: data.groupId
        }
    });
};

// 解除拉黑
export const removeFromBlacklist = (studentId: number) => {
    return ajax.delete('/black/delete', {
        params: { studentId }
    });
};

// 加入小组
export const joinGroup = (studentId: number, groupId: number) => {
    return ajax.post("/student/join", null, {
        params: {
            studentId,
            groupId
        }
    });
};

// 退出小组
export const exitGroup = (studentId: number, groupId: number) => {
    return ajax.post("/student/exit", null,{
        params: {
            studentId,
            groupId
        }
    });
};

// 获取院系列表
export const getDepartmentList = () => {
    return ajax.get<ApiResponse<DataModel[]>>("/data/getList");
};

// 根据院系获取专业
export const getMajorListByDepartment = (department: string) => {
    return ajax.get<ApiResponse<DataModel[]>>("/data/getByID", {
        params: { department }
    });
};

// 获取学生事件日志
export const getStudentEvents = (studentId: number) => {
    return ajax.get<ApiResponse<EventLog[]>>("/student/getLog", {
        params: { id: studentId }
    });
};

// 搜索学生
export const searchStudents = (data: any) => {
    // 提取分页参数和搜索条件
    const { pageNum, pageSize, ...searchParams } = data;
    return ajax.post("/student/Search", searchParams, {
        params: {
            pageNum,
            pageSize
        }
    });
};

// 重置密码
export const resetPassword = (id: number, password: string) => {
    return ajax.post("/student/resetPassword", null, {
        params: { id, password }
    });
};